package com.gxl.butt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.DAO.IHardUserDAO;
import com.gxl.butt.domain.HardUser;
import com.gxl.butt.dto.bo.User;
import com.gxl.butt.exception.CException;
import com.gxl.butt.service.IHardUserService;
import com.gxl.butt.dto.vo.IPBean;
import com.gxl.butt.dto.vo.UserBean;
import com.gxl.butt.utils.HttpUtils;
import com.gxl.butt.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gxl.butt.value.ErrorMsg.*;
import static com.gxl.butt.value.StaticValue.WX_APPID;
import static com.gxl.butt.value.StaticValue.WX_SECRET;

/**
 * Created by guoxiaolin on 2017/6/6.
 */
@Service(interfaceClass = IHardUserService.class,retries = 0)
@Component
public class HardUserServiceImpl implements IHardUserService {

    @Autowired
    private IHardUserDAO iHardUserDAO;


    @Override
    @Transactional
    public JSONObject registerUser(String account, String password, String nickname, String idcard){

        Integer hasReg = iHardUserDAO.selectUserByAccount(account);
        if (hasReg >= 1){
            return JSONUtils.getRoot(-1,ACCOUNT_EXIST,STR_IS_EMPTY);
        }

        List<HardUser> hardUsers = iHardUserDAO.selectUserByIdCard(idcard);
        if (hardUsers!=null){
            return JSONUtils.getRoot(-2,IDCARD_BE_REGISTER,STR_IS_EMPTY);
        }

        Integer hasone = iHardUserDAO.insertUser(account, password, nickname,idcard);
        return JSONUtils.getRoot(0,SUCCESS_MSG,STR_IS_EMPTY);

    }

    @Override
    @Transactional
    public JSONObject loginUser(String account, String password){

        List<HardUser> hardUsers = iHardUserDAO.selectUserByAccountAndPassword(account, password);
        HardUser hardUser = hardUsers.get(0);

        UserBean userBean = new UserBean();
        userBean.setNickname(hardUser.getNickname()).setAccount(hardUser.getAccount()).setId(hardUser.getId()).setIdcard(hardUser.getIdcard());

        if (hardUsers == null||hardUsers.size() == 0){
            return JSONUtils.getRoot(-1,FAILD_MSG,STR_IS_EMPTY);
        }

        return JSONUtils.getRoot(0, SUCCESS_MSG,userBean);

    }

    @Override
    @Transactional
    public JSONObject checkAccount(String account){
        Integer hasReg = iHardUserDAO.selectUserByAccount(account);
        if (hasReg >= 1){
            return JSONUtils.getRoot(-1,ACCOUNT_EXIST,STR_IS_EMPTY);
        }
        return JSONUtils.getRoot(0,ACCOUNT_NOT_EXIST,STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public String getTestBeanList() {

        IPBean ipBean = new IPBean();
        ipBean.setId(1l).setIP("0.0.0.0").setUserid(2l);
        List<IPBean> ipBeans = new ArrayList<IPBean>();
        ipBeans.add(ipBean);
        return ipBeans.toString();

    }

    @Override
    @Transactional
    public JSONObject getTenUser() {
        List<HardUser> list = iHardUserDAO.getTenUser();
        JSONArray jsonArray = new JSONArray();
        for (int i =0 ;i<list.size();i++){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("avatarUrl", list.get(i).getAvatarUrl());
            jsonArray.add(jsonObject);
        }
        return JSONUtils.getRoot(0, SUCCESS_MSG, jsonArray);
    }

    @Override
    @Transactional
    public JSONObject xcxLogin(User user) {

        Long duid = iHardUserDAO.selectUserByOpenid(user.getOpenid());
        if (duid != null && duid != 0L){
            return JSONUtils.getRoot(0, ACCOUNT_EXIST, duid);
        }
        HardUser hu = new HardUser();
        hu.setAccount(user.getOpenid());
        hu.setPassowrd("123456");
        hu.setIdcard("000000000000000000");
        hu.setNickname(user.getNickname());
        hu.setAvatarUrl(user.getAvatarurl());
        hu.setCity(user.getCity());
        hu.setCountry(user.getCountry());
        hu.setProvince(user.getProvince());
        hu.setGender(user.getGender());
        hu.setLanguage(user.getLanguage());
        hu.setOpenid(user.getOpenid());
        Long  uid = iHardUserDAO.insertUser(hu);
        return JSONUtils.getRoot(0, SUCCESS_MSG, uid);

    }
}
